<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;

use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\Orderqueue;
use think\Db;

/**
 * 技师房控制器
 * @package app\index\controller
 */
class EngineerRoom extends Auth
{
    public $noNeedLogin = [];

    /**
     * 获取钟房所有用户数据
     */
    public function index(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            $where['oq.store_id'] = $userInfo['store_id'];
            //状态筛选
            if(!empty($getData['work_status'])){
                $where['u.work_status'] = $getData['work_status'];
            }
            //班次筛选
            if(!empty($getData['work_type'])){
                $where['u.work_type'] = $getData['work_type'];
            }

            //技师工号搜索
            if(!empty($getData['work_number'])){
                $where['u.work_number'] = $getData['work_number'];
            }
            //性别筛选
            if(!empty($getData['sex'])){
                $where['u.sex'] = $getData['sex'];
            }
            //工种筛选
            if(!empty($getData['top_type_id'])){
                $where['u.top_type_id'] = $getData['top_type_id'];
            }
            if(!isset($getData['sort_type'])){
                $getData['sort_type'] = '0';
            }
            if($getData['sort_type'] == '1'){
                //工号排序
                $order = 'u.work_number asc';
            }elseif ($getData['sort_type'] == '2'){
                //下钟时间
                $order = 'oq.real_time desc';
            }else{
                //默认钟房顺序
                $order = 'oq.sort  asc';
            }
            $sql = Db::name('qt_order_queue')
                ->field('oq.id,u.id as user_id,u.work_number,u.sex,wt.title as name,oq.room_id,oq.sort,u.work_type,u.work_status,st.title as work_title,oq.head,oq.foot,oq.start_time,oq.end_time,u.top_type_id')
                ->alias('oq')
                ->leftJoin('qt_users u','u.id = oq.user_id')
                ->leftJoin('qt_work_type wt','wt.id = u.top_type_id')
                ->leftJoin('qt_scheduling_type st','st.id = u.work_type')
                ->where($where)
                ->where(['u.group'=>'1','u.status'=>'0']);
            //项目筛选
            if(!empty($getData['project_id'])){
                $sql->leftJoin('qt_user_project up','up.engineer_id =  u.id')
                    ->where('up.project_id',$getData['project_id']);
            }
            //总数据
            $originalData = $sql->order($order)->select();
            foreach ($originalData as $key=>$value){
                //判断副项是否计算排钟
                if($this->storeInfo['is_second_queue_add_block'] == '1'){
                    //副项算里面
                    $originalData[$key]['clock'] = Db::name('qt_order_project')
                        ->where(['engineer_id'=>$value['user_id'],'order_type'=>'2'])
                        ->where('is_delete',2)
                        ->whereTime('created_time','today')
                        ->count();
                    $originalData[$key]['row'] = Db::name('qt_order_project')
                        ->where('is_delete',2)
                        ->where(['engineer_id'=>$value['user_id'],'order_type'=>'1'])
                        ->whereTime('created_time','today')
                        ->count();
                }else{
                    //副项不算里面
                    $originalData[$key]['clock'] = Db::name('qt_order_project')
                        ->alias('op')
                        ->leftJoin('qt_project p','p.id = op.project_id')
                        ->where(['op.engineer_id'=>$value['user_id'],'p.type'=>'1'])
                        ->where('op.is_delete',2)
                        ->whereIn('op.order_type',['2','3'])
                        ->whereTime('created_time','today')
                        ->count();
                    $originalData[$key]['row'] = Db::name('qt_order_project')
                        ->alias('op')
                        ->leftJoin('qt_project p','p.id = op.project_id')
                        ->where(['op.engineer_id'=>$value['user_id'],'p.type'=>'1'])
                        ->where('op.is_delete',2)
                        ->whereIn('op.order_type',['1','4'])
                        ->whereTime('created_time','today')
                        ->count();
                }
                $room = Db::name('qt_room')
                    ->field('r.*,s.name as type_name,s.color as room_color')
                    ->alias('r')
                    ->leftJoin('qt_room_status s ','s.id = r.status_id')
                    ->where(['r.id'=>$value['room_id']])
                    ->find();
                $project = Db::name('qt_order_project')
                    ->field('op.*,p.id as project,p.name as project_name,p.time')
                    ->alias('op')
                    ->leftJoin('qt_project p','op.project_id = p.id')
                    ->where(['op.room_id'=>$value['room_id'],'op.engineer_id'=>$value['user_id']])
                    ->where('op.is_delete',2)
                    ->order('id desc')
                    ->find();
                $originalData[$key]['room'] = $room['name'];
                $originalData[$key]['color'] = '';
                $originalData[$key]['room_color'] = Db::name('qt_work_status_color')->where('id',$value['work_status'])->value('color');
                if($value['work_status'] == '6'){
                    $originalData[$key]['room_color'] = 'rgb(148,173,50)';
                }
                if($value['work_status'] == '7'){
                    $originalData[$key]['room_color'] = 'rgb(254,152,0)';
                }
                $originalData[$key]['project_id'] = $project['id'];//项目订单ID
                $originalData[$key]['time'] = $project['time'];
                $originalData[$key]['project_type'] = $project['order_type'];
                $originalData[$key]['project_status'] = $project['status'];
                $originalData[$key]['project'] = $project['project'];//项目ID
                $originalData[$key]['project_name'] = $project['project_name'];//项目名称
                $originalData[$key]['key'] = $key;//键值对
                $originalData[$key]['add'] = 0;//加钟数量
                //技师是否有预约
                $appointment = Db::name('qt_room_appointment')->where(['engineer_id'=>$value['user_id'],'status'=>'1'])->find();
                if(empty($appointment)){
                    $originalData[$key]['appointment'] = 0;//没有预约
                }else{
                    $originalData[$key]['appointment'] = 1;//有预约
                    if($value['work_status'] == '1'){
                        $originalData[$key]['room_color'] = Db::name('qt_work_status_color')->where('id','3')->value('color');
                    }
                }
                if(!empty($project) && $project['order_type'] == '2'){
                    $originalData[$key]['room_color'] = 'rgb(210,50,45)';
                }
            }
            //分开数据
            static $group = [];
            $list = Db::name('qt_work_type')
                ->where(['store_id'=>$this->userInfo['store_id'],'pid'=>'0'])
                ->field('id,title')
                ->select();
            foreach ($list as $key=>$value){
                $group[] = $value;
                $child = Db::name('qt_work_type')
                    ->where(['store_id'=>$this->userInfo['store_id'],'pid'=>$value['id']])
                    ->field('id,title')
                    ->select();
                foreach ($child as $k=>$val){
                    $group[] = $val;
                }

            }
            foreach ($group as $key=>$value){
                //总数据
                $userRes = Db::name('qt_order_queue')
                    ->field('oq.id,u.id as user_id,u.work_number,u.sex,wt.title as name,oq.room_id,oq.sort,u.work_type,u.work_status,st.title as work_title,oq.head,oq.foot,oq.start_time,oq.end_time')
                    ->alias('oq')
                    ->leftJoin('qt_users u','u.id = oq.user_id')
                    ->leftJoin('qt_work_type wt','wt.id = u.top_type_id')
                    ->leftJoin('qt_scheduling_type st','st.id = u.work_type')
                    ->where('u.top_type_id',$value['id'])
                    ->where($where)
                    ->where(['u.role'=>'3','u.store_id'=>$this->userInfo['store_id'],'u.status'=>'0'])
                    ->order($order)
                    ->select();
                foreach ($userRes as $k=>$v){
                    //判断副项是否计算排钟
                    if($this->storeInfo['is_second_queue_add_block'] == '1'){
                        $userRes[$k]['clock'] = Db::name('qt_order_project')
                            ->where(['engineer_id'=>$v['user_id'],'order_type'=>'2'])
                            ->where('is_delete',2)
                            ->whereTime('created_time','today')
                            ->count();
                        $userRes[$k]['row'] = Db::name('qt_order_project')
                            ->where('is_delete',2)
                            ->where(['engineer_id'=>$v['user_id'],'order_type'=>'1'])
                            ->whereTime('created_time','today')
                            ->count();
                    }else{
                        //副项不算里面
                        $userRes[$k]['clock'] = Db::name('qt_order_project')
                            ->alias('op')
                            ->leftJoin('qt_project p','p.id = op.project_id')
                            ->where(['op.engineer_id'=>$v['user_id'],'p.type'=>'1'])
                            ->where('op.is_delete',2)
                            ->whereIn('op.order_type',['2','3'])
                            ->whereTime('created_time','today')
                            ->count();
                        $userRes[$k]['row'] = Db::name('qt_order_project')
                            ->alias('op')
                            ->leftJoin('qt_project p','p.id = op.project_id')
                            ->where(['op.engineer_id'=>$v['user_id'],'p.type'=>'1'])
                            ->whereIn('op.order_type',['1','4'])
                            ->where('op.is_delete',2)
                            ->whereTime('created_time','today')
                            ->count();
                    }
                    $room = Db::name('qt_room')
                        ->field('r.*,s.name as type_name,s.color as room_color')
                        ->alias('r')
                        ->leftJoin('qt_room_status s ','s.id = r.status_id')
                        ->where(['r.id'=>$v['room_id']])
                        ->find();
                    $project = Db::name('qt_order_project')
                        ->field('op.*,p.id as project,p.name as project_name,p.time')
                        ->alias('op')
                        ->leftJoin('qt_project p','op.project_id = p.id')
                        ->where('op.is_delete',2)
                        ->where(['op.room_id'=>$v['room_id'],'op.engineer_id'=>$v['user_id']])
                        ->order('id desc')
                        ->find();
                    $userRes[$k]['room'] = $room['name'];
                    $userRes[$k]['color'] = '';
                    $userRes[$k]['room_color'] = Db::name('qt_work_status_color')->where('id',$v['work_status'])->value('color');
                    if($v['work_status'] == '6'){
                        $userRes[$k]['room_color'] = 'rgb(148,173,50)';
                    }
                    if($v['work_status'] == '7'){
                        $userRes[$k]['room_color'] = 'rgb(254,152,0)';
                    }
                    $userRes[$k]['project_id'] = $project['id'];
                    $userRes[$k]['time'] = $project['time'];
                    $userRes[$k]['project_type'] = $project['order_type'];
                    $userRes[$k]['project_status'] = $project['status'];
                    $userRes[$k]['project'] = $project['project'];//项目ID
                    $userRes[$k]['project_name'] = $project['project_name'];//项目名称
                    $userRes[$k]['key'] = $k;//键值对
                    //技师是否有预约
                    $appointment = Db::name('qt_room_appointment')->where(['engineer_id'=>$v['user_id'],'status'=>'1'])->count();
                    $userRes[$k]['appointment'] = 0;//没有预约
                    if($appointment > 0){
                        $userRes[$k]['appointment'] = 1;//有预约
                        if($v['work_status'] == '1'){
                            $userRes[$k]['room_color'] = Db::name('qt_work_status_color')->where('id','3')->value('color');
                        }
                    }
                    if(!empty($project) && $project['order_type'] == '2'){
                        $userRes[$k]['room_color'] = 'rgb(210,50,45)';
                    }
                }
                $group[$key]['data'] = $userRes;
            }
            $result['original'] = $originalData;
            $result['group'] = $group;
            $result['total'] = Db::name('qt_users')
                ->where(['group'=>'1','store_id'=>$this->userInfo['store_id'],'status'=>'0'])
                ->count();//总技师
            $result['free'] = Db::name('qt_users')
                ->where(['group'=>'1','store_id'=>$this->userInfo['store_id'],'status'=>'0','work_status'=>'1'])
                ->count();//空闲技师
            $result['no_work'] = Db::name('qt_users')
                ->where(['group'=>'1','store_id'=>$this->userInfo['store_id'],'status'=>'0','work_status'=>'2'])
                ->count();//未上班
            $result['work'] = Db::name('qt_users')
                ->where(['group'=>'1','store_id'=>$this->userInfo['store_id'],'status'=>'0'])
                ->whereIn('work_status',['3','4','5'])
                ->count();//上钟技师
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 排序
     */
    public function sort(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            $list =  Db::name('qt_order_queue')->field('id,sort')
                ->where('sort','neq','0')
                ->where('store_id',$userInfo['store_id'])
                ->order('sort asc')
                ->select();
            //获取老用户的坐标位置
            $oldSort  = Db::name('qt_order_queue')->where('id',$getData['old_sort'])->value('sort');
            $oldNumber = Db::name('qt_order_queue')->where('sort','<',$oldSort)->count();
            //获取新用户的坐标位置
            $newSort  = Db::name('qt_order_queue')->where('id',$getData['new_sort'])->value('sort');
            $newNumber = Db::name('qt_order_queue')->where('sort','<',$newSort)->count();
            $oldSort = $oldNumber;
            $newSort = $newNumber;
            //id 主键 sort 排序值
            //id集合
            $idArr   = [];
            //排序集合
            $sortArr = [];
            foreach ($list as $item) {
                $idArr[]   = $item['id'];
                $sortArr[] = $item['sort'];
            }
            //记录要拖动的id
            $oldValue = $idArr[$oldSort];
            //删除这个要拖动的id
            unset($idArr[$oldSort]);
            //插入新的位置，并自动移位
            array_splice($idArr, $newSort, 0, $oldValue);
            //重新设置排序
            $set = [];
            for ($i = 0; $i < count($idArr); $i++) {
                $set[$i]['id']   = $idArr[$i];
                $set[$i]['sort'] = $sortArr[$i];
            }
            foreach ($set as $key=>$value){
                Db::name('qt_order_queue')->where('id',$value['id'])->update(['sort'=>$value['sort']]);
            }
            //保存到数据库
            $this->result('排序成功', '', 200);
        }

    }


    /**
     * 多种排位排序
     */
    public function many_sort(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            $list =  Db::name('qt_order_many_queue')->field('id,sort')
                ->where('sort','neq','0')
                ->where('store_id',$userInfo['store_id'])
                ->where(['many_queue_id'=>$getData['many_queue_id']])
                ->order('sort asc')
                ->select();
            //获取老用户的坐标位置
            $oldSort  = Db::name('qt_order_many_queue')->where('id',$getData['old_sort'])->where(['many_queue_id'=>$getData['many_queue_id']])->value('sort');
            $oldNumber = Db::name('qt_order_many_queue')->where('sort','<',$oldSort)->where(['many_queue_id'=>$getData['many_queue_id']])->count();
            //获取新用户的坐标位置
            $newSort  = Db::name('qt_order_many_queue')->where('id',$getData['new_sort'])->where(['many_queue_id'=>$getData['many_queue_id']])->value('sort');
            $newNumber = Db::name('qt_order_many_queue')->where('sort','<',$newSort)->where(['many_queue_id'=>$getData['many_queue_id']])->count();
            $oldSort = $oldNumber;
            $newSort = $newNumber;
            //id 主键 sort 排序值
            //id集合
            $idArr   = [];
            //排序集合
            $sortArr = [];
            foreach ($list as $item) {
                $idArr[]   = $item['id'];
                $sortArr[] = $item['sort'];
            }
            //记录要拖动的id
            $oldValue = $idArr[$oldSort];
            //删除这个要拖动的id
            unset($idArr[$oldSort]);
            //插入新的位置，并自动移位
            array_splice($idArr, $newSort, 0, $oldValue);
            //重新设置排序
            $set = [];
            for ($i = 0; $i < count($idArr); $i++) {
                $set[$i]['id']   = $idArr[$i];
                $set[$i]['sort'] = $sortArr[$i];
            }
            //dump($set);die;
            foreach ($set as $key=>$value){
                Db::name('qt_order_many_queue')->where('id',$value['id'])->where(['many_queue_id'=>$getData['many_queue_id']])->update(['sort'=>$value['sort']]);
            }
            //保存到数据库
            $this->result('排序成功', '', 200);
        }

    }


    /**
     * 头牌或者尾排
     */

    public function topOrTail()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            $list = Db::name('qt_order_queue')->field('id,sort')
                ->where('sort', 'neq', '0')
                ->where('store_id', $userInfo['store_id'])
                ->order('sort asc')
                ->select();
            //获取老用户的坐标位置
            $oldSort = Db::name('qt_order_queue')->where('user_id', $getData['user_id'])->value('sort');
            $oldNumber = Db::name('qt_order_queue')
                ->where(['store_id'=>$this->userInfo['store_id']])
                ->where('sort', '<', $oldSort)
                ->count();
            //头牌
            if($getData['type'] == '1'){
                $newNumber = '0';
            }
            //尾牌
            if($getData['type'] == '2'){
                $newNumber = Db::name('qt_order_queue')
                    ->where('sort', 'neq', '0')
                    ->where(['store_id'=>$this->userInfo['store_id']])
                    ->count();
            }
            $oldSort = $oldNumber;
            $newSort = $newNumber;
            //id 主键 sort 排序值
            //id集合
            $idArr = [];
            //排序集合
            $sortArr = [];
            foreach ($list as $item) {
                $idArr[] = $item['id'];
                $sortArr[] = $item['sort'];
            }
            //记录要拖动的id
            $oldValue = $idArr[$oldSort];
            //删除这个要拖动的id
            unset($idArr[$oldSort]);
            //插入新的位置，并自动移位
            array_splice($idArr, $newSort, 0, $oldValue);
            //重新设置排序
            $set = [];
            for ($i = 0; $i < count($idArr); $i++) {
                $set[$i]['id'] = $idArr[$i];
                $set[$i]['sort'] = $sortArr[$i];
            }
            foreach ($set as $key => $value) {
                Db::name('qt_order_queue')->where('id', $value['id'])->update(['sort' => $value['sort']]);
            }
            //保存到数据库
            $this->result('操作成功', '', 200);
        }
    }



    /**
     * 获取多种排位列表
     */
    public function many_index(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            if(empty($getData['many_queue_id'])){
                $this->result('缺少多种排位参数', '', 204);
            }
            $where['oq.store_id'] = $userInfo['store_id'];
            //状态筛选
            if(!empty($getData['work_status'])){
                $where['u.work_status'] = $getData['work_status'];
            }
            //班次筛选
            if(!empty($getData['work_type'])){
                $where['u.work_type'] = $getData['work_type'];
            }

            //技师工号搜索
            if(!empty($getData['work_number'])){
                $where['u.work_number'] = $getData['work_number'];
            }
            //性别筛选
            if(!empty($getData['sex'])){
                $where['u.sex'] = $getData['sex'];
            }
            //工种筛选
            if(!empty($getData['top_type_id'])){
                $where['u.top_type_id'] = $getData['top_type_id'];
            }
            if(!isset($getData['sort_type'])){
                $getData['sort_type'] = '0';
            }
            if($getData['sort_type'] == '1'){
                //工号排序
                $order = 'u.work_number asc';
            }elseif ($getData['sort_type'] == '2'){
                //下钟时间
                $order = 'oq.real_time desc';
            }else{
                //默认钟房顺序
                $order = 'oq.sort  asc';
            }
            $sql = Db::name('qt_order_many_queue')
                ->field('oq.id,u.id as user_id,u.work_number,u.sex,wt.title as name,queue.room_id,oq.sort,u.work_type,u.work_status,st.title as work_title,queue.start_time,queue.end_time,u.top_type_id')
                ->alias('oq')
                ->leftJoin('qt_users u','u.id = oq.user_id')
                ->leftJoin('qt_work_type wt','wt.id = u.top_type_id')
                ->leftJoin('qt_scheduling_type st','st.id = u.work_type')
                ->leftJoin('qt_order_queue queue','queue.user_id = oq.user_id')
                ->where($where)
                ->where(['u.role'=>'3','u.status'=>'0','oq.many_queue_id'=>$getData['many_queue_id']]);
            //项目筛选
            if(!empty($getData['project_id'])){
                $sql->leftJoin('qt_user_project up','up.engineer_id =  u.id')
                    ->where('up.project_id',$getData['project_id']);
            }
            //总数据
            $originalData = $sql->order($order)
                ->select();
            $originalData = unique_2d_array_by_key($originalData,'user_id');
            foreach ($originalData as $key=>$value){
                //判断副项是否计算排钟
                if($this->storeInfo['is_second_queue_add_block'] == '1'){
                    //副项算里面
                    $originalData[$key]['clock'] = Db::name('qt_order_project')
                        ->where(['engineer_id'=>$value['user_id'],'order_type'=>'2'])
                        ->where('is_delete',2)
                        ->whereTime('created_time','today')
                        ->count();
                    $originalData[$key]['row'] = Db::name('qt_order_project')
                        ->where('is_delete',2)
                        ->where(['engineer_id'=>$value['user_id'],'order_type'=>'1'])
                        ->whereTime('created_time','today')
                        ->count();
                }else{
                    //副项不算里面
                    $originalData[$key]['clock'] = Db::name('qt_order_project')
                        ->alias('op')
                        ->leftJoin('qt_project p','p.id = op.project_id')
                        ->where(['op.engineer_id'=>$value['user_id'],'p.type'=>'1'])
                        ->whereIn('op.order_type',['2','3'])
                        ->where('op.is_delete',2)
                        ->whereTime('created_time','today')
                        ->count();
                    $originalData[$key]['row'] = Db::name('qt_order_project')
                        ->alias('op')
                        ->leftJoin('qt_project p','p.id = op.project_id')
                        ->where(['op.engineer_id'=>$value['user_id'],'p.type'=>'1'])
                        ->where('op.is_delete',2)
                        ->whereIn('op.order_type',['1','4'])
                        ->whereTime('created_time','today')
                        ->count();
                }
                $room = Db::name('qt_room')
                    ->field('r.*,s.name as type_name,s.color as room_color')
                    ->alias('r')
                    ->leftJoin('qt_room_status s ','s.id = r.status_id')
                    ->where(['r.id'=>$value['room_id']])
                    ->find();
                $project = Db::name('qt_order_project')
                    ->field('op.*,p.id as project,p.name as project_name,p.time')
                    ->alias('op')
                    ->leftJoin('qt_project p','op.project_id = p.id')
                    ->where('op.is_delete',2)
                    ->where(['op.room_id'=>$value['room_id'],'op.engineer_id'=>$value['user_id']])
                    ->order('id desc')
                    ->find();
                $originalData[$key]['room'] = $room['name'];
                $originalData[$key]['color'] = '';
                $originalData[$key]['room_color'] = Db::name('qt_work_status_color')->where('id',$value['work_status'])->value('color');
                if($value['work_status'] == '6'){
                    $originalData[$key]['room_color'] = 'rgb(148,173,50)';
                }
                if($value['work_status'] == '7'){
                    $originalData[$key]['room_color'] = 'rgb(254,152,0)';
                }
                $originalData[$key]['project_id'] = $project['id'];//项目订单ID
                $originalData[$key]['time'] = $project['time'];
                $originalData[$key]['project_type'] = $project['order_type'];
                $originalData[$key]['project_status'] = $project['status'];
                $originalData[$key]['project'] = $project['project'];//项目ID
                $originalData[$key]['project_name'] = $project['project_name'];//项目名称
                $originalData[$key]['key'] = $key;//键值对
                $originalData[$key]['add'] = 0;//加钟数量
                //技师是否有预约
                $appointment = Db::name('qt_room_appointment')->where(['engineer_id'=>$value['user_id'],'status'=>'1'])->find();
                if(empty($appointment)){
                    $originalData[$key]['appointment'] = 0;//没有预约
                }else{
                    $originalData[$key]['appointment'] = 1;//有预约
                    if($value['work_status'] == '1'){
                        $originalData[$key]['room_color'] = Db::name('qt_work_status_color')->where('id','3')->value('color');
                    }
                }
                if(!empty($project) && $project['order_type'] == '2' && $value['work_status'] != '1'){
                    $originalData[$key]['room_color'] = 'rgb(210,50,45)';
                }
            }
            //分开数据
            static $group = [];
            $list = Db::name('qt_work_type')
                ->where(['store_id'=>$this->userInfo['store_id'],'pid'=>'0'])
                ->field('id,title')
                ->select();
            foreach ($list as $key=>$value){
                $group[] = $value;
                $child = Db::name('qt_work_type')
                    ->where(['store_id'=>$this->userInfo['store_id'],'pid'=>$value['id']])
                    ->field('id,title')
                    ->select();
                foreach ($child as $k=>$val){
                    $group[] = $val;
                }

            }
            foreach ($group as $key=>$value){
                //总数据
                $userRes = Db::name('qt_order_many_queue')
                    ->field('oq.id,u.id as user_id,u.work_number,u.sex,wt.title as name,queue.room_id,oq.sort,u.work_type,u.work_status,st.title as work_title,queue.start_time,queue.end_time')
                    ->alias('oq')
                    ->leftJoin('qt_users u','u.id = oq.user_id')
                    ->leftJoin('qt_work_type wt','wt.id = u.top_type_id')
                    ->leftJoin('qt_scheduling_type st','st.id = u.work_type')
                    ->leftJoin('qt_order_queue queue','queue.user_id = oq.user_id')
                    ->where('u.top_type_id',$value['id'])
                    ->where($where)
                    ->where(['u.role'=>'3','u.store_id'=>$this->userInfo['store_id'],'u.status'=>'0','oq.many_queue_id'=>$getData['many_queue_id']])
                    ->order($order)
                    ->select();
                $userRes = unique_2d_array_by_key($userRes,'user_id');
                foreach ($userRes as $k=>$v){
                    //判断副项是否计算排钟
                    if($this->storeInfo['is_second_queue_add_block'] == '1'){
                        $userRes[$k]['clock'] = Db::name('qt_order_project')
                            ->where('is_delete',2)
                            ->where(['engineer_id'=>$v['user_id'],'order_type'=>'2'])
                            ->whereTime('created_time','today')
                            ->count();
                        $userRes[$k]['row'] = Db::name('qt_order_project')
                            ->where('is_delete',2)
                            ->where(['engineer_id'=>$v['user_id'],'order_type'=>'1'])
                            ->whereTime('created_time','today')
                            ->count();
                    }else{
                        //副项不算里面
                        $userRes[$k]['clock'] = Db::name('qt_order_project')
                            ->alias('op')
                            ->leftJoin('qt_project p','p.id = op.project_id')
                            ->where(['op.engineer_id'=>$v['user_id'],'p.type'=>'1'])
                            ->where('op.is_delete',2)
                            ->whereIn('op.order_type',['2','3'])
                            ->whereTime('created_time','today')
                            ->count();
                        $userRes[$k]['row'] = Db::name('qt_order_project')
                            ->alias('op')
                            ->leftJoin('qt_project p','p.id = op.project_id')
                            ->where(['op.engineer_id'=>$v['user_id'],'p.type'=>'1'])
                            ->where('op.is_delete',2)
                            ->whereIn('op.order_type',['1','4'])
                            ->whereTime('created_time','today')
                            ->count();
                    }
                    $room = Db::name('qt_room')
                        ->field('r.*,s.name as type_name,s.color as room_color')
                        ->alias('r')
                        ->leftJoin('qt_room_status s ','s.id = r.status_id')
                        ->where(['r.id'=>$v['room_id']])
                        ->find();
                    $project = Db::name('qt_order_project')
                        ->field('op.*,p.id as project,p.name as project_name,p.time')
                        ->alias('op')
                        ->leftJoin('qt_project p','op.project_id = p.id')
                        ->where('op.is_delete',2)
                        ->where(['op.room_id'=>$v['room_id'],'op.engineer_id'=>$v['user_id']])
                        ->order('id desc')
                        ->find();
                    $userRes[$k]['room'] = $room['name'];
                    $userRes[$k]['color'] = '';
                    $userRes[$k]['room_color'] = Db::name('qt_work_status_color')->where('id',$v['work_status'])->value('color');
                    if($v['work_status'] == '6'){
                        $userRes[$k]['room_color'] = 'rgb(148,173,50)';
                    }
                    if($v['work_status'] == '7'){
                        $userRes[$k]['room_color'] = 'rgb(254,152,0)';
                    }
                    $userRes[$k]['project_id'] = $project['id'];
                    $userRes[$k]['time'] = $project['time'];
                    $userRes[$k]['project_type'] = $project['order_type'];
                    $userRes[$k]['project_status'] = $project['status'];
                    $userRes[$k]['project'] = $project['project'];//项目ID
                    $userRes[$k]['project_name'] = $project['project_name'];//项目名称
                    $userRes[$k]['key'] = $k;//键值对
                    //技师是否有预约
                    $appointment = Db::name('qt_room_appointment')->where(['engineer_id'=>$v['user_id'],'status'=>'1'])->find();
                    if(empty($appointment)){
                        $userRes[$k]['appointment'] = 0;//没有预约
                    }else{
                        $userRes[$k]['appointment'] = 1;//有预约
                        if($v['work_status'] == '1'){
                            $userRes[$k]['room_color'] = Db::name('qt_work_status_color')->where('id','3')->value('color');
                        }

                    }
                    if(!empty($project) && $project['order_type'] == '2' && $v['work_status'] != '1'){
                        $userRes[$k]['room_color'] = 'rgb(210,50,45)';
                    }
                }
                $group[$key]['data'] = $userRes;
            }
            $result['original'] = $originalData;
            $result['group'] = $group;
            $result['total'] = Db::name('qt_users')
                ->where(['role'=>'3','store_id'=>$this->userInfo['store_id'],'status'=>'0'])
                ->count();//总技师
            $result['free'] = Db::name('qt_users')
                ->where(['role'=>'3','store_id'=>$this->userInfo['store_id'],'status'=>'0','work_status'=>'1'])
                ->count();//空闲技师
            $result['no_work'] = Db::name('qt_users')
                ->where(['role'=>'3','store_id'=>$this->userInfo['store_id'],'status'=>'0','work_status'=>'2'])
                ->count();//未上班
            $result['work'] = Db::name('qt_users')
                ->where(['role'=>'3','store_id'=>$this->userInfo['store_id'],'status'=>'0'])
                ->whereIn('work_status',['3','4','5'])
                ->count();//上钟技师
            $this->result('查询成功', $result, 200);
        }
    }




    /**
     * 多种排位头牌或者尾排
     */
    public function many_top_or_tail()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            $list = Db::name('qt_order_many_queue')->field('id,sort')
                ->where('sort', 'neq', '0')
                ->where('store_id', $userInfo['store_id'])
                ->where(['many_queue_id'=>$getData['many_queue_id']])
                ->order('sort asc')
                ->select();
            //获取老用户的坐标位置
            $oldSort = Db::name('qt_order_many_queue')->where('user_id', $getData['user_id'])->where(['many_queue_id'=>$getData['many_queue_id']])->value('sort');
            $oldNumber = Db::name('qt_order_many_queue')
                ->where(['store_id'=>$this->userInfo['store_id']])
                ->where('sort', '<', $oldSort)
                ->where(['many_queue_id'=>$getData['many_queue_id']])
                ->count();
            //头牌
            if($getData['type'] == '1'){
                $newNumber = '0';
            }
            //尾牌
            if($getData['type'] == '2'){
                $newNumber = Db::name('qt_order_many_queue')
                    ->where(['many_queue_id'=>$getData['many_queue_id']])
                    ->where('sort', 'neq', '0')
                    ->where(['store_id'=>$this->userInfo['store_id']])
                    ->count();
            }
            $oldSort = $oldNumber;
            $newSort = $newNumber;
            //id 主键 sort 排序值
            //id集合
            $idArr = [];
            //排序集合
            $sortArr = [];
            foreach ($list as $item) {
                $idArr[] = $item['id'];
                $sortArr[] = $item['sort'];
            }
            //记录要拖动的id
            $oldValue = $idArr[$oldSort];
            //删除这个要拖动的id
            unset($idArr[$oldSort]);
            //插入新的位置，并自动移位
            array_splice($idArr, $newSort, 0, $oldValue);
            //重新设置排序
            $set = [];
            for ($i = 0; $i < count($idArr); $i++) {
                $set[$i]['id'] = $idArr[$i];
                $set[$i]['sort'] = $sortArr[$i];
            }
            foreach ($set as $key => $value) {
                Db::name('qt_order_many_queue')->where('id', $value['id'])->where(['many_queue_id'=>$getData['many_queue_id']])->update(['sort' => $value['sort']]);
            }
            //保存到数据库
            $this->result('操作成功', '', 200);
        }
    }






}
